package cn.bdqn.controller;

import cn.bdqn.domain.Address;
import cn.bdqn.service.AddressService;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 行政划区控制层
 * @author dkj
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    /**
     * 行政划区业务接口
     */
    @Autowired
    private AddressService addressService;

    /**
     * 查询全部的父级元素
     * @return
     */
    @RequestMapping("/queryByParent")
    public Result queryByParent() throws Exception{
        try{
            //查询所有父级元素
            List<Address> addresses = addressService.queryAllParent();
            //返回结果
            return ResultGenerator.genSuccessResult(addresses);
        }catch (Exception e){
            e.printStackTrace();
            //失败
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据父级id查询子级数据
     * @param parentId
     * @return
     */
    @RequestMapping("/queryByParentId")
    public Result queryByParentId(Integer parentId)throws Exception{

        try{
            //根据父级id查询子级数据.sss
            List<Address> addresses = addressService.queryByParentId(parentId);
            //返回结果
            return  ResultGenerator.genSuccessResult(addresses);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());

        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/queryByAddressId")
    public Result queryByAddressId(Integer id)throws Exception{
        try{
            //根据父级id查询子级数据.sss
            Address addresses = addressService.queryByAddressId(id);
            //返回结果
            return  ResultGenerator.genSuccessResult(addresses);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
}
